package com.learning.jquizenglish.screen.viewcontroller;

import java.util.ArrayList;

import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

import com.learning.jquizenglish.R;
import com.learning.jquizenglish.common.JQuizTaskFragment.JQuizTaskListener;
import com.learning.jquizenglish.model.DoQuizSession;
import com.learning.jquizenglish.model.JQuizRequestModel;
import com.learning.jquizenglish.model.QuestionModel;
import com.learning.jquizenglish.network.JQuizEnglishApi;
import com.learning.jquizenglish.network.JQuizEnglishApi.Level;
import com.learning.jquizenglish.network.JQuizEnglishApi.QuestionType;
import com.learning.jquizenglish.screen.DoQuizScreen;
import com.learning.jquizenglish.screen.DoQuizScreen.DoQuizMode;

public class QuestionNumberView {

	private Context mContext;
	private TextView txtTitle;
	private TextView txtTutorial;
	private TextView txtInfo;
	private Spinner spNumberQuestion;
	private ImageButton btnStart;
	private DoQuizScreen mDoQuizScreen;
	private int numberOfQuestion = 10;
	
	public QuestionNumberView(DoQuizScreen fragScreen) {
		mDoQuizScreen = fragScreen;
	}
	
	public void init(Context context,final View contentView){
		mContext = context;
		txtTitle 	= (TextView) contentView.findViewById(R.id.tvTitle);
		txtTutorial = (TextView) contentView.findViewById(R.id.tv_tutorial);
		txtInfo		= (TextView) contentView.findViewById(R.id.tv_info);
		spNumberQuestion = (Spinner) contentView.findViewById(R.id.sp_noq);
		btnStart 	= (ImageButton) contentView.findViewById(R.id.btn_start_take_quiz);
		btnStart.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				btnStart.setVisibility(View.GONE);
				contentView.findViewById(R.id.pb).setVisibility(View.VISIBLE);
				QuestionType questionType = DoQuizSession.getInstance().getQuestionType();
				Level level = DoQuizSession.getInstance().getQuestionLevel();
				JQuizRequestModel requestModel = JQuizEnglishApi.getRequestQuestion(questionType, level, numberOfQuestion);
				mDoQuizScreen.executeGetTaskReturnList(requestModel, false, QuestionModel.class, new JQuizTaskListener(){

					@Override
					public void onTaskComplete(Object object) {
						if(object!=null){
							DoQuizSession.getInstance().setListQuestions((ArrayList<QuestionModel>) object);
							mDoQuizScreen.changeMode(DoQuizMode.DO_QUIZ);
						}
					}
					
				});
			}
		});
		initData();
	}
	
	private void initData(){
		txtTitle.setText(mContext.getResources().getString(R.string.str_english_grammar)+"\n Elementary");
		txtTutorial.setText(mContext.getResources().getString(R.string.str_comment_tutorial));
		txtInfo.setText(mContext.getResources().getString(R.string.str_info));
		initSpinnerData();
	}
	
	private void initSpinnerData(){
		final ArrayList<Integer> arrNumberQuestion = new ArrayList<Integer>();
		arrNumberQuestion.add(Integer.valueOf(10));
		arrNumberQuestion.add(Integer.valueOf(15));
		arrNumberQuestion.add(Integer.valueOf(20));
		arrNumberQuestion.add(Integer.valueOf(25));
	    ArrayAdapter<Integer> spAdapter = new ArrayAdapter<Integer>(mContext, R.layout.spinner_item, arrNumberQuestion);
	    spAdapter.setDropDownViewResource(R.layout.spinner_item);
	    spNumberQuestion.setAdapter(spAdapter);
	    spNumberQuestion.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int pos, long arg3) {
				numberOfQuestion = arrNumberQuestion.get(pos);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				
			}
		});
	}

}
